package guinong.com.gnw.ui.activty;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.Bind;
import butterknife.OnClick;
import guinong.com.gnw.R;
import guinong.com.gnw.base.BaseActivity;
import guinong.com.gnw.mvp.Login_Presenter;
import guinong.com.gnw.mvp.Login_View;
import guinong.com.gnw.utils.CommonUtils;
import guinong.com.gnw.utils.Constant;


/**
 * @author chenshuangniu
 * @time 2016/11/11 14:12
 * @content
 * @amend
 * @viersion 1.0
 */
public class LoginActivity extends BaseActivity<Login_Presenter> implements Login_View {
    @Bind(R.id.edt_phone)
    EditText edtPhone;
    @Bind(R.id.edt_password)
    EditText edtPassword;
    @Bind(R.id.btn_confirm)
    Button btnConfirm;
    @Bind(R.id.tv_register)
    TextView tvRegister;
    @Bind(R.id.tv_show_red)
    TextView testToast;
    private String phoneStr = "";
    private String passwordStr = "";

    /**
     * 是否第一次登录 这里主要是为了配合保存数据存入的数组
     */
    private String mIsFristLogin = Constant.NO;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void setTitle() {

    }

    @Override
    protected void initPresenter() {
        super.initPresenter();
        mPresenter = new Login_Presenter(this, this);
    }

    @Override
    public void clickButtonListener(String name, View v) {

    }

    @Override
    public void getInputStringListener(String btnName, String[] inputs) {

    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
      /*  if (mShareDataUtils.getIsFristLogin()) {
            startActivity(MainActivity.class);
        }
        if (mShareDataUtils.getUserLoginInfo() != null) {
            edtPhone.setText(mShareDataUtils.getUserLoginInfo()[0]);
            edtPassword.setText(mShareDataUtils.getUserLoginInfo()[1]);
        }*/
    }

    //----------------------------eventBus接收事件start-------------------------------------
    /*@Subscribe(threadMode = ThreadMode.MAIN)
    public void getPhone(SuccessFresh phone) {
        edtPhone = (EditText) findViewById(R.id.edt_phone);
        edtPhone.setText(phone.getMsg());
    }*/


    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        phoneStr = savedInstanceState.getString("type");
        if (CommonUtils.isEmpty(phoneStr)) {
            edtPhone.setText(phoneStr);
        }
    }

    @Override
    public void enterMain() {
        startActivity(MainActivity.class);
    }

    @Override
    public void showRedToast(String msg) {
        testToast.setText(msg);
    }

    @OnClick({R.id.btn_confirm, R.id.tv_register})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_confirm:
                if (checkOk()) {
                    mPresenter.loginPost(phoneStr, passwordStr);
                }
                break;
            case R.id.tv_register:
               // startActivity(RegisterActivity.class);
                break;
        }
    }

    private boolean checkOk() {
        phoneStr = edtPhone.getText().toString().trim();
        passwordStr = edtPassword.getText().toString().trim();
        if (CommonUtils.isEmpty(phoneStr)) {
            mPresenter.putShowRedToast(getResources().getString(R.string.toast_input_phone));
            return false;
        }
        if (!CommonUtils.isMobileNO(phoneStr)) {
            mPresenter.putShowRedToast(getResources().getString(R.string.toast_input_right_phone));
            return false;
        }
        if (CommonUtils.isEmpty(passwordStr)) {
            mPresenter.putShowRedToast(getResources().getString(R.string.toast_input_password));
            return false;
        }
        mPresenter.putShowRedToast(getResources().getString(R.string.toast_input_right));
        return true;
    }
}
